package com.blockmeta.bbs.businesslibrary.umeng;

import android.content.Context;
import android.graphics.Bitmap;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.blockmeta.bbs.baselibrary.utils.ToastUtil;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * @author AirThor
 * @date 2020/3/24.
 * 分享生成图片或者保存图片的帮助类
 */
public final class ShareResTask {
    /**
     * 开启子线程并在主线程回调结果
     * @param task 任务
     * @param <T> 任务产物
     */
    public static <T>void doTask(final Task<T> task){
        Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(ObservableEmitter<T> emitter) throws Exception {
                emitter.onNext(task.doInBackground());
                emitter.onComplete();
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new DefaultObserver<T>() {
            @Override
            public void onNext(T t) {
                task.doOnUIThread(t);
            }

            @Override
            public void onError(Throwable e) {
                task.doOnUIThread(null);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    /**
     * 生成一般view的截图任务
     */
    public abstract static class NormalBitmapTask implements Task<Bitmap>{

        private View mView;
        public NormalBitmapTask(View view) {
            mView = view;
        }

        @Override
        public Bitmap doInBackground() {
            //缩放至宽固定的大小
            return ScreenShotUtil.shotView(mView);
        }

    }

    /**
     * 生成RecyclerView的截图任务
     */
    public abstract static class RecyclerBitmapTask implements Task<Bitmap>{

        private int itemCount;
        private RecyclerView mRecyclerView;
        public RecyclerBitmapTask(RecyclerView recyclerView) {
            this(20,recyclerView);
        }

        public RecyclerBitmapTask(int itemCount, RecyclerView recyclerView) {
            this.itemCount = itemCount;
            mRecyclerView = recyclerView;
        }

        @Override
        public Bitmap doInBackground() {
            return ScreenShotUtil.shotRecyclerView(mRecyclerView,itemCount);
        }

    }

    /**
     * 保存图片任务
     */
    public static class SaveImgTask implements Task<Object>{

        private Bitmap mBitmap;
        private Context mContext;
        public SaveImgTask(Context context,Bitmap bitmap) {
            mBitmap = bitmap;
            mContext = context;
        }

        @Override
        public Object doInBackground() {
            ScreenShotUtil.savePicture(mContext,mBitmap,100);
            return 1;
        }

        @Override
        public void doOnUIThread(Object o) {
            ToastUtil.show("图片保存成功");
        }

    }

    public interface Task<T>{
        /**
         * 在子线程做耗时任务
         * @return T
         */
        T doInBackground();

        /**
         * 主线程回调
         * @param t 。
         */
        void doOnUIThread(T t);
    }
}
